package com.echoesnet.echoesshortvideo.Utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import com.echoesnet.echoesshortvideo.Api.ApiException;
import com.echoesnet.echoesshortvideo.Api.HttpMethods;
import com.echoesnet.echoesshortvideo.Api.RequestHeader;
import com.echoesnet.echoesshortvideo.Api.Subscriber.SilenceSubscriber;
import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Constant.Api;

import java.util.Map;

/**
 * @author yqh
 * @Date 2018/4/26
 * @Version 1.0
 */
public class NetUtil
{
    /**
     * 获取网络状态
     *
     * @param context
     * @return 返回值 -1：没有网络  1：WIFI网络2：wap网络3：net网络
     */
    public static int getNetworkStatus(Context context)
    {
        int netType = -1;
        if (context == null)
            context = AppController.getInstance();
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context
                .CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo == null)
        {
            return netType;
        }
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE)
        {
            if (!TextUtils.isEmpty(networkInfo.getExtraInfo()))
            {
                if ("cmnet".equals(networkInfo.getExtraInfo().toLowerCase()))
                {
                    netType = 3;
                }
                else
                {
                    netType = 2;
                }
            }
        }
        else if (nType == ConnectivityManager.TYPE_WIFI)
        {
            netType = 1;
        }
        return netType;
    }

    /**
     * 检查是否绑定手机
     * @param netUtilListener
     */
    public static void checkIsBindMobile(NetUtilListener netUtilListener){
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        HttpMethods.getInstance().startServerRequest(new SilenceSubscriber<String>(){
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                if (netUtilListener != null)
                    netUtilListener.onSuccess();
            }

            @Override
            public void onHandledError(ApiException apiE)
            {
                if (netUtilListener != null)
                    netUtilListener.onFail();
            }
        }, Api.LOGINC_CHECK,reqMap,true);
    }
}
